package ar.com.sgsvm.view.lineaFactura;

import java.math.BigDecimal;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import ar.com.sgsvm.R;
import ar.com.sgsvm.entity.Articulo;
import ar.com.sgsvm.entity.Factura;
import ar.com.sgsvm.entity.LineaFactura;
import ar.com.sgsvm.logic.ArticuloLogica;
import ar.com.sgsvm.logic.FacturaLogica;
import ar.com.sgsvm.logic.LineaFacturaLogica;

public class LineaFacturaAdapter extends ArrayAdapter<LineaFactura> {

	private Context mContext;
	private List<LineaFactura> mLineasFacturas;
//	private List<LineaFactura> mAllLineasFacturas;

	public LineaFacturaAdapter(Context context, int textViewResourceId, List<LineaFactura> lineasFactura) {
		super(context, textViewResourceId, lineasFactura);
		mContext = context;
		mLineasFacturas = lineasFactura;
//		mAllLineasFacturas = lineasFactura;
	}

	@Override
	public int getCount() {
		return mLineasFacturas.size();
	}

	@Override
	public LineaFactura getItem(int position) {
		return mLineasFacturas.get(position);
	}

	// @Override
	// public Filter getFilter() {
	// return new FilterLineaFactura();
	// }
	//
	// public void sort(int which, int asc) {
	// Collections.sort(mLineasFacturas, new LineaFacturaComparator(which,
	// asc));
	// notifyDataSetChanged();
	// }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.lyt_lineafactura_completa, null);
			holder.txtCantidadLineaFactura = (TextView) convertView.findViewById(R.id.txtCantidadLineaFactura);
			holder.txtDescripcionLineaFactura = (TextView) convertView.findViewById(R.id.txtDescripcionLineaFactura);
			holder.txtPrecioLineaFactura = (TextView) convertView.findViewById(R.id.txtPrecioLineaFactura);
			holder.txtDescuentoLineaFactura = (TextView) convertView.findViewById(R.id.txtDescuentoLineaFactura);
			holder.txtTotalLineaFactura = (TextView) convertView.findViewById(R.id.txtTotalLineaFactura);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		LineaFactura lineaFactura = getItem(position);
		holder.txtCantidadLineaFactura.setText(String.valueOf(lineaFactura.getCantidad()));
		ArticuloLogica articuloLogica = new ArticuloLogica();
		try {
			Articulo articulo = articuloLogica.obtener(lineaFactura.getArticulo().getIdArticulo());
			holder.txtDescripcionLineaFactura.setText(String.valueOf(articulo.getDescripcion()));
			FacturaLogica facturaLogica = new FacturaLogica();
			Factura factura = facturaLogica.obtenerFacturaPorIdLineaFactura(lineaFactura.getIdLineaFactura());
			LineaFacturaLogica lineaFacturaLogica = new LineaFacturaLogica();
			BigDecimal precio = lineaFacturaLogica.determinarPrecio(articulo.getIdArticulo(), factura.getFechaHora());
			holder.txtPrecioLineaFactura.setText(String.valueOf(precio));
			BigDecimal descuento = lineaFactura.getDescuento();
			holder.txtDescuentoLineaFactura.setText(String.valueOf(descuento));
			holder.txtTotalLineaFactura.setText(String.valueOf(precio.subtract(precio.multiply(descuento))));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return convertView;
	}

	private static class ViewHolder {
		TextView txtCantidadLineaFactura;
		TextView txtDescripcionLineaFactura;
		TextView txtPrecioLineaFactura;
		TextView txtDescuentoLineaFactura;
		TextView txtTotalLineaFactura;
	}
}
